package com.service.thirddatacenter.hpool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.service.thirddatacenter.hpool.model.entity.AccountThirdEntity;
import com.service.thirddatacenter.hpool.service.AccountThirdService;
import com.service.thirddatacenter.hpool.service.BasePlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.service.thirddatacenter.hpool.mapper.AccountMinerMapper;
import com.service.thirddatacenter.hpool.model.entity.AccountMinerEntity;
import com.service.thirddatacenter.hpool.service.AccountMinerService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 矿机
 *
 * @date 2021-06-08 15:41:03
 */
@Slf4j
@Service("accountMinerService")
public class AccountMinerServiceImpl extends ServiceImpl<AccountMinerMapper, AccountMinerEntity> implements AccountMinerService {

    @Autowired
    private AccountThirdService accountThirdService;

    @Autowired
    private BasePlatformService hpoolService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncMinerFromPlatform() {

        List<AccountThirdEntity> accountThirdList = accountThirdService.listAllAccount();
        if (Objects.isNull(accountThirdList)) {
            log.info("暂无第三方平台账号，获取矿池完成");
            return;
        }
        accountThirdList.parallelStream().forEach(account->{
            switch (account.getPlatform()){
                case "hpool":
                    hpoolService.syncMiner(account);
                    return;
                default:
                    log.info("{}未实现", account);
                    return;
            }
        });
    }

    @Override
    public AccountMinerEntity getByPlatformMinerId(String platformMinerId) {
        return getOne(Wrappers.lambdaQuery(AccountMinerEntity.class)
                .eq(AccountMinerEntity::getPlatformMinerId, platformMinerId)
                .last("limit 1"));
    }

    @Override
    public List<AccountMinerEntity> listByAccountIds(List<Integer> accountIds) {
        return list(new QueryWrapper<AccountMinerEntity>().lambda()
            .in(AccountMinerEntity::getAccountId, accountIds)
        );
    }

    /**
     * @param accountIds
     * @param minerType
     * @param minerStatus
     * @Description: 统计商家所有某类型矿机的在线算力
     * @Author: ty
     * @Date: 2021/6/22 15:38
     * @param: [accountIds, minerType]
     * @return: java.math.BigDecimal
     */
    @Override
    public BigDecimal totalOnline(List<Integer> accountIds, String minerType, Integer minerStatus) {
        return baseMapper.totalOnline(accountIds, minerType, minerStatus);
    }

}
